package com.platform.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.platform.dao.ApiReturnMapper;
import com.platform.dao.ApiUserLevelMapper;
import com.platform.dao.ApiUserMapper;
import com.platform.entity.*;
import com.platform.utils.DateUtils;
import com.platform.utils.R;
import com.platform.utils.RRException;
import com.platform.utils.StringUtils;
import com.platform.validator.Assert;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ApiReturnService {
    @Autowired
    private ApiReturnMapper apiReturnMapper;

    @Autowired
    private ApiOrderGoodsService apiOrderGoodsService;

    @Autowired
    private ApiShippingService apiShippingService;

    @Autowired
    private ApiOrderService apiOrderService;

    @Autowired
    private ApiGoodsService apiGoodsService;

    @Autowired
    private ApiMacroService apiMacroService;

    @Transactional
    public R submit(JSONObject map){
        Integer orderGoodsId = map.getInteger("id");
        String reasonCode =  map.getString("reasonCode");
        Integer type =  map.getInteger("type");
        Integer number =  map.getInteger("number");

        Integer shippingId =  map.getInteger("shippingId");
        String shippingNo =  map.getString("shippingNo");
        String remark =  map.getString("remark");
        JSONArray pics =  map.getJSONArray("picUrls");

        if (pics == null || pics.size() == 0){
            return R.error("请上传附件！");
        }

        if (orderGoodsId == null){
            return R.error("无效的商品！");
        }

        if (number == null || number.intValue() == 0){
            return R.error("请选择退货数量！");
        }

        OrderGoodsVo orderGoodsVo = apiOrderGoodsService.queryObject(orderGoodsId);
        if (orderGoodsVo == null){
            return R.error("无效的商品！");
        }

        if (number.intValue() > orderGoodsVo.getNumber()){
            return R.error("退货数量不能大于购买数量！");
        }

        if (StringUtils.isNullOrEmpty(reasonCode)){
            return R.error("请选择退回原因！");
        }

        if (StringUtils.isNullOrEmpty(type)){
            return R.error("请选择退货方式！");
        }

        ShippingVo shippingVo = null;

        ReturnVo returnVo = new ReturnVo();

        if (type.equals(ReturnVo.TYPE_RG)){
            //退货
            if (shippingId == null){
                return R.error("请选择快递公司！");
            }

            shippingVo = apiShippingService.queryObject(shippingId);
            if (shippingVo == null){
                return R.error("无效的快递公司！");
            }

            if (StringUtils.isNullOrEmpty(shippingNo)){
                return R.error("请输入快递单号！");
            }

            returnVo.setShipping_id(shippingVo.getId());
            returnVo.setShipping_code(shippingVo.getCode());
            returnVo.setShipping_name(shippingVo.getName());
            returnVo.setShipping_no(shippingNo);
        }

        OrderVo orderVo = apiOrderService.queryObject(orderGoodsVo.getOrder_id());

        GoodsVo goodsVo = apiGoodsService.queryObject(orderGoodsVo.getGoods_id());

        if (orderVo.getShipping_status().equals(1) || orderVo.getShipping_status().equals(2)){
            //已发货，判断时间是否支持发货
            Date sendDate = orderVo.getShipping_time();
            if (DateUtils.addDate(sendDate, goodsVo.getMax_return_day()).compareTo(new Date()) < 0){
                return R.error("该商品超过最大退货期限，无法退货！");
            }
        }

        //是否存在未审核的退货
        int count = apiReturnMapper.queryTotalAudit(orderVo.getId(), ReturnVo.AUDIT_STATUS_INIT);
        if (count > 0){
            return R.error("存在未审核的退货单，无法再次申请！");
        }
        orderGoodsVo.setIs_return(OrderGoodsVo.IS_RETURN);
        apiOrderGoodsService.update(orderGoodsVo);

        returnVo.setNumber(number);
        returnVo.setOrder_goods_id(orderGoodsId.longValue());
        returnVo.setOrder_id(orderVo.getId().longValue());
        returnVo.setApply_time(new Date());
        returnVo.setType(type);
        returnVo.setReason_code(reasonCode);
        String returnReasonDesc = apiMacroService.getNameByKeyAndValue("order_goods_return_reason", reasonCode).getName();
        returnVo.setReason_desc(returnReasonDesc);
        returnVo.setRemark(remark);

        returnVo.setStatus(ReturnVo.STATUS_DEAL);
        returnVo.setAudit_status(ReturnVo.AUDIT_STATUS_INIT);

        for (int i = 0; i < pics.size(); i++){
            JSONObject item = pics.getJSONObject(i);
            if (i == 0){
                returnVo.setPic1_url(item.getString("url"));
            } else if (i == 1){
                returnVo.setPic2_url(item.getString("url"));
            } else if (i == 2){
                returnVo.setPic3_url(item.getString("url"));
            } else if (i == 3){
                returnVo.setPic4_url(item.getString("url"));
            } else if (i == 4){
                returnVo.setPic5_url(item.getString("url"));
            } else if (i == 5){
                returnVo.setPic6_url(item.getString("url"));
            }
        }


        apiReturnMapper.save(returnVo);
        return R.ok();
    }
}
